// main.go
package main

import (
	"device_manager/config"
	"device_manager/database"
	"device_manager/models"
	"device_manager/theme"
	"device_manager/ui"
	"log"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	_ "gorm.io/gorm"
)

func main() {
	a := app.New()
	a.Settings().SetTheme(&theme.MyTheme{})
	w := a.NewWindow("设备管理软件")

	// 初始化数据库
	db, err := database.InitDB()
	if err != nil {
		log.Fatal("无法连接数据库", err)
	}

	// 自动迁移
	db.AutoMigrate(&models.Device{}, &models.Log{}, &config.Config{})

	// 加载配置
	cfg, err := config.LoadConfig(db)
	if err != nil {
		log.Println("加载配置失败，使用默认配置")
		cfg = &config.Config{}
	}

	// 创建选项卡
	tabs := container.NewAppTabs(
		container.NewTabItem("门禁", ui.CreateAccessControlTab(db, cfg)),
		container.NewTabItem("电源空开", ui.CreatePowerSwitchTab(db, cfg)),
		container.NewTabItem("云喇叭", ui.CreateCloudSpeakerTab(db, cfg)),
		container.NewTabItem("设备管理", ui.CreateDeviceManagementTab(db)),
		container.NewTabItem("设置", ui.CreateSettingsTab(db, cfg)),
	)

	w.SetContent(tabs)
	w.Resize(fyne.NewSize(1000, 700))
	w.ShowAndRun()
}
